package com.example.springboot.controller;

import com.example.springboot.common.MD5Util;
import com.example.springboot.common.Result;
import com.example.springboot.controller.DTO.User2QueryDTO;
import com.example.springboot.controller.DTO.UserQueryDTO;
import com.example.springboot.dao.UserDao;
import com.example.springboot.dao.mUserDao;
import com.example.springboot.entity.User;
import com.example.springboot.entity.User2;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user2")
public class User2Controller {
    @Autowired
    private UserDao userDao;

    @GetMapping
    private Result GetByUsername(String username, String password){
        String pw = MD5Util.MD5Encode(password, "UTF-8");
        User2 user2 = userDao.getByUsername(username, pw);
        if(user2 == null)return Result.error("用户不存在");
        else return Result.success(user2);
    }

    @PostMapping
    private Result Save(@RequestBody User2 user2){
        String pw = MD5Util.MD5Encode(user2.getPassword(), "UTF-8");
        user2.setPassword(pw);
        User2 user = userDao.getByUsername(user2.getUsername(), pw);
        if(user != null) return Result.error("用户名已存在");
        else return Result.success(userDao.insert(user2));
    }

    @PostMapping("/login")
    public Result Login(@RequestBody User2 user2){
        if(user2.getUsername()==null||user2.getPassword()==null){
            //throw new RuntimeException("参数不可为空");
            return Result.error("参数不可为空");
        }
        String pw = MD5Util.MD5Encode(user2.getPassword(), "UTF-8");
        User2 res =  userDao.getByUsername(user2.getUsername(), pw);
        if(res==null){
            //throw new RuntimeException("用户名密码错误");
            return Result.error("用户名密码错误");
        }
        return Result.success(res);
    }
    @GetMapping("/page")// /user/page?currentPage= & pageSize=
    public Result findPage(User2QueryDTO userQueryDTO){

        if(userQueryDTO.getCurrentPage()==null||userQueryDTO.getCurrentPage()<=0||userQueryDTO.getPageSize()<1){
            return Result.error("参数错误");
        }
        Integer currentPage = userQueryDTO.getCurrentPage();
        Integer pageSize = userQueryDTO.getPageSize();
        PageHelper.startPage(currentPage, pageSize);
        List<User2> users = userDao.findByOptions(userQueryDTO);
        return Result.success(new PageInfo<>(users));
    }

    @DeleteMapping("/{id}")
    public Result Delete(@PathVariable Integer id){
        if(id==null||id==0){
            //throw new RuntimeException("参数不可为空");
            Result.error("参数不可为空");
        }
        return Result.success(userDao.deleteById(id)==1);
    }


    @PutMapping
    public Result Updata(@RequestBody User2 user){
        if(user.getId()==null){
            //throw new RuntimeException("参数不可为空");
            return Result.error("参数不可为空");
        }
        String pw = MD5Util.MD5Encode(user.getPassword(), "UTF-8");
        user.setPassword(pw);
        return Result.success(userDao.updata(user));
    }
}
